﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Configuration;

namespace Joe.Coin.Monitor.App
{
    public partial class frmMain : Form
    {
        Thread dataTh;
        Action<bool> actUpdateDisplay;
        IList<DispWrapper> dispList, loopList;
        public frmMain()
        {
            InitializeComponent();
            dispList = new List<DispWrapper>();
            loopList = new List<DispWrapper>();

            MonCoinsSection cfgSection = ConfigurationManager.GetSection("monCoins") as MonCoinsSection;
            foreach (CoinSection coin in cfgSection.CoinCollection) {
                var obj = new DispWrapper();
                obj.CoinName = coin.CoinName;
                obj.MonTypeObj = coin.MonType;
                obj.Url = coin.Url;
                obj.LowTips = coin.LowTipsPrice;
                obj.HighTips = coin.HighTipsPrice;
                dispList.Add(obj);
                loopList.Add(obj);
            }

            bs.AllowNew = false;
            bs.DataSource = dispList;
            
            var coinNameCol = new DataGridViewTextBoxColumn();
            coinNameCol.HeaderText = "Coin Name";
            coinNameCol.DataPropertyName = "CoinName";
            coinNameCol.ValueType = typeof(string);
            dataGridView1.Columns.Add(coinNameCol);

            var monTypeCol = new DataGridViewTextBoxColumn();
            monTypeCol.HeaderText = "Monitor Type";
            monTypeCol.DataPropertyName = "MonTypeObj";
            monTypeCol.ValueType = typeof(MonType);
            dataGridView1.Columns.Add(monTypeCol);

            var timeCol = new DataGridViewTextBoxColumn();
            timeCol.HeaderText = "Update Time";
            timeCol.DataPropertyName = "UpdateTime";
            timeCol.ValueType = typeof(DateTime);
            timeCol.DefaultCellStyle.Format = "HH:mm:ss";
            dataGridView1.Columns.Add(timeCol);

            var buyCol = new DataGridViewTextBoxColumn();
            buyCol.HeaderText = "Buy Price";
            buyCol.DataPropertyName = "BuyPrice";
            buyCol.ValueType = typeof(decimal);
            dataGridView1.Columns.Add(buyCol);

            var sellCol = new DataGridViewTextBoxColumn();
            sellCol.HeaderText = "Sell Price";
            sellCol.DataPropertyName = "SellPrice";
            sellCol.ValueType = typeof(decimal);
            dataGridView1.Columns.Add(sellCol);

            var lowCol = new DataGridViewTextBoxColumn();
            lowCol.HeaderText = "Low Tips Price";
            lowCol.DataPropertyName = "LowTips";
            lowCol.ValueType = typeof(decimal);
            dataGridView1.Columns.Add(lowCol);

            var highCol = new DataGridViewTextBoxColumn();
            highCol.HeaderText = "High Tips Price";
            highCol.DataPropertyName = "HighTips";
            highCol.ValueType = typeof(decimal);
            dataGridView1.Columns.Add(highCol);

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = bs;

            actUpdateDisplay = new Action<bool>((alert) => {
                bs.ResetBindings(false);
                if (alert)
                {
                    this.dataGridView1.BackgroundColor = Color.Orange;
                }
                else {
                    this.dataGridView1.BackgroundColor = Color.LightGray;
                }
            });

            dataTh = new Thread(DoGetPrice);
            dataTh.IsBackground = true;
            dataTh.Start();
        }

        void DoGetPrice() {
            Thread.Sleep(1000);
            while (true)
            {
                int updateCount = 0;
                bool alert = false;
                foreach (var dispObj in loopList)
                {
                    try
                    {
                        var obj = WebReq.GetByUrl<RspWrapper>(dispObj.Url);
                        dispObj.UpdateTime = DateTime.Now;
                        //obj.sellprice为平台卖出价值,即我们的买入价
                        dispObj.BuyPrice = obj.SellPrice;
                        //obj.buyprice为平台买入价值,即我们的卖出价
                        dispObj.SellPrice = obj.BuyPrice;
                        alert = alert || ((dispObj.MonTypeObj ==MonType.ForBuy ? dispObj.BuyPrice : dispObj.SellPrice) >= dispObj.HighTips || dispObj.BuyPrice <= dispObj.LowTips);
                        updateCount++;
                        Thread.Sleep(100);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                    }
                }
                if (updateCount > 0) {
                    this.BeginInvoke(actUpdateDisplay, alert);
                }
                Thread.Sleep(10000);
            }
        }

        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            var obj = (DispWrapper)dataGridView1.Rows[e.RowIndex].DataBoundItem;
            if (obj.Alert) {
                dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
            }
        }
    }
}
